package com.aos.erp.schools.rpc.student;

import com.aos.erp.schools.cutils.Constant;
import com.aos.erp.schools.enums.student.FieldStatus;
import com.aos.erp.schools.enums.student.StudentStatus;
import com.aos.erp.schools.enums.student.SubjectType;
import com.aos.erp.schools.model.student.Student;
import com.aos.erp.schools.model.student.StudentField;
import com.aos.erp.schools.model.student.StudentFieldHistory;
import com.aos.erp.schools.model.student.dto.AddStudentFieldDto;
import com.aos.erp.schools.model.student.dto.StudentOneDto;
import com.aos.erp.schools.model.student.ro.student.UpdateStudentRo;
import com.aos.erp.schools.service.student.StudentFieldHistoryService;
import com.aos.erp.schools.service.student.StudentFieldService;
import com.aos.erp.schools.service.student.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Auther: chb
 * @Date: 2018/11/1 19:22
 * @Description:    学员成绩
 */
@Slf4j
@RestController
@RequestMapping("/service/studentField")
@Api(tags = {"学员成绩"})
public class StudentFieldServiceController {

    @Resource
    private StudentFieldService studentFieldService;

    @Resource
    private StudentFieldHistoryService studentFieldHistoryService;

    @Resource
    private StudentService studentService ;


    @PostMapping("/add")
    @ApiOperation(value = "学员成绩添加")
    public boolean addStudentPicture(@RequestBody List<AddStudentFieldDto> ro){

//        List<AddStudentFieldDto> ro =new LinkedList<>();
//        AddStudentFieldDto aaa=new AddStudentFieldDto();
//        aaa.setName("张大大22");
//        aaa.setDriverId(480);
//        aaa.setExamCarModel("C1");
//        aaa.setExamTime(new Date());
//        aaa.setExamAddress("测试考点");
//        aaa.setExamSubject(1);
//        aaa.setFieldStatus(FieldStatus.ok);
//        ro.add(aaa);
        //
        //name=, examSubject=1, examCarModel=C1, examTime=Thu Nov 29 00:00:00 CST 2018, examAddress=测试考点, examField=90, fieldStatus=ok, driverId=480
        //需要对数据 进行过滤
        // 1.保存到成绩历史表 和成绩表
        // 2.根据成绩  修改学员状态
        log.info("学员成绩添加:" + ro);
        try {
            List<StudentFieldHistory> listHistory=new LinkedList<>();
            for(AddStudentFieldDto asr : ro)
            {
                StudentOneDto sdto=new StudentOneDto();
                sdto.setName(asr.getName());
                sdto.setDriverId(asr.getDriverId());
               //查找这个学员
                Student student=studentService.selectOne(sdto);
                if(student==null)
                {
                    continue;
                }
                StudentField se=new StudentField();
                BeanUtils.copyProperties(asr,se);
                se.setStudentId(student.getId());
                studentFieldService.saveBean(se);//保存到 学员成绩表中

                //给历史表的记录 封装数据
                StudentFieldHistory sfh=new StudentFieldHistory();
                BeanUtils.copyProperties(asr,sfh);
                sfh.setStudentId(student.getId());
                listHistory.add(sfh);

            }
            studentFieldHistoryService.saveBatch(listHistory);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


  
}
